package com.project.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.bean.OrderBean;
import com.project.bean.ShopCarBean;
import com.project.mapper.IOrderMapper;
import com.project.mapper.IShopCarMapper;
import com.project.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

/**
 * @author lyl
 * description:订单
 */
@Service
@Transactional
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private IOrderMapper iOrderMapper;
    @Autowired
    private IShopCarMapper iShopCarMapper;

    @Override
    public PageInfo<OrderBean> findOrderByItem(int pageNo, String orderStatue, LocalDate startDate, LocalDate endDate, Integer userId) {
        PageHelper.startPage(pageNo, 3);
        PageInfo<OrderBean> info = PageInfo.of(iOrderMapper.findOrderByItem(orderStatue, startDate, endDate, userId));
        return info;
    }


    @Override
    public PageInfo<OrderBean> findOrderByStatue(int pageNo, String orderStatue, LocalDate startDate, LocalDate endDate) {
        PageHelper.startPage(pageNo, 3);
        PageInfo<OrderBean> info = PageInfo.of(iOrderMapper.findOrderByItem(orderStatue, startDate, endDate, null));
        return info;
    }

    @Override
    public OrderBean findByOrderId(int orderId) {
        return iOrderMapper.findByOrderId(orderId);
    }

    @Override
    public void addNewOrder(OrderBean orderBean, int userId) {
        //首先进行添加新订单对象操作
        iOrderMapper.addNewOrder(orderBean, userId);
        //查找购物车集合
        List<ShopCarBean> list = iShopCarMapper.findAllByUserId(userId);
        for (ShopCarBean shopCarBean : list) {
            iOrderMapper.addOrderInfo(shopCarBean.getProductBean().getProductId(), orderBean.getOrderId(), shopCarBean.getNum(), shopCarBean.getProductBean().getProductPrice());
            System.out.println("当前购物车商品信息" + shopCarBean.getProductBean());
        }

        for (ShopCarBean shopCarBean : list) {
            iShopCarMapper.cleanShopCar(userId);
        }
    }

    @Override
    public void proUserUpdateOrderStatue(int orderId) {
        iOrderMapper.updateOrderStatue(orderId, "发货中");
    }

    @Override
    public void postUserUpdateOrderStatue(int orderId) {
        iOrderMapper.updateOrderStatue(orderId, "已完结");
    }
}
